package com.edu.zju.scs.panacea.service.resource;

import com.alibaba.fastjson.JSON;
import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.resource.Template;
import com.edu.zju.scs.panacea.mapper.resource.TemplateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-19 18:41
 */
@Service
public class TemplateService {

    @Autowired
    private TemplateMapper templateMapper;

    public BaseResult getTemplate(Long id) {
        BaseResult baseResult = new BaseResult();
        Template template = templateMapper.selectTemplate(id);
        if (template == null) {
            baseResult.error("无此id对应的模板");
        } else {
            baseResult.success(template);
        }
        return baseResult;
    }

    public BaseResult getAllTemplate() {
        BaseResult baseResult = new BaseResult();
        baseResult.success(templateMapper.selectAllTemplate());
        return baseResult;
    }

    public BaseResult deleteTemplate(Long id) {
        BaseResult baseResult = new BaseResult();
        long deleteNums = templateMapper.deleteTemplate(id);
        if (deleteNums == 0) {
            baseResult.error("删除失败！");
        } else {
            baseResult.success(deleteNums);
        }
        return baseResult;
    }

    public BaseResult addTemplate(Template template) {
        BaseResult baseResult = new BaseResult();
        Long insertNums = templateMapper.insertTemplate(template);
        if (insertNums == null) {
            baseResult.error("新增素材模板失败！");
        } else {
            baseResult.success(template.getId());
        }
        return baseResult;
    }

    public BaseResult editTemplate(Template template) {
        BaseResult baseResult = new BaseResult();
        Long updateNums = templateMapper.updateTemplate(template);
        if (updateNums == null) {
            baseResult.error("更新素材模板失败！");
        } else {
            baseResult.success(updateNums);
        }
        return baseResult;
    }

    public BaseResult getTemplateBySiteId(Long siteId) {
        BaseResult baseResult = new BaseResult();
        List<Template> templateList = templateMapper.selectTemplateBySiteId(siteId);

        if (templateList == null) {
            baseResult.error("查找该站点下的模板失败！");
        } else {
            baseResult.success(templateList);
        }
        return baseResult;
    }


    public BaseResult getTemplateByLocs(List<Long> locIds, Integer type) {
        BaseResult baseResult = new BaseResult();
        List<Template> templateList = templateMapper.selectTemplateByLocs(locIds, type);
        if (templateList == null) {
            baseResult.error("查找模板失败！");
        } else {
            baseResult.success(templateList);
        }
        return baseResult;
    }
}
